import { Graph, Line, Path, Curve } from '@antv/x6'

export const multiSmooth = Graph.registerConnector(
    'multi-smooth',
    (
        sourcePoint,
        targetPoint,
        routePoints,
        options: { raw?: boolean; index?: number; total?: number; gap?: number },
    ) => {
      const { index = 1, total = 6, gap = 24 } = options
      const line = new Line(sourcePoint, targetPoint)
      const centerIndex = (total - 1) / 2
      const dist = index - centerIndex
      const diff = Math.abs(dist)
      const factor = diff === 0 ? 1 : diff / dist
      const vertice = line
          .pointAtLength(line.length() / 2 + gap * factor * Math.ceil(diff))
          .rotate(90, line.getCenter())

      const points = [sourcePoint, vertice, targetPoint]
      const curves = Curve.throughPoints(points)
      const path = new Path(curves)
      return options.raw ? path : path.serialize()
    },
    true,
)